import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { getCorpTypeList } from '/@/api/busi/corp';
import { tenantList, updateTaskStatus} from "/@/api/busi/record";



export const miningTenantId = async (miningTenant:string) => {
  const result = await tenantList({page: 1, size: 1000, type: 'COMPANY'})
  const list = result.returnData.list
  const res = list.find((e => {
    return e.miningTenant = miningTenant;
  }))
  if (res) {
    return res.id;
  }
}

export const columns: BasicColumn[] = [
  {
    title: '检查表名称',
    dataIndex: 'name',
    fixed: 'left',
  },
  {
    title: '企业类别',
    dataIndex: 'id',
    slots: { customRender: 'accompanyType' },
  },
  {
    title: '企业名称',
    dataIndex: 'accompanyTenant',
  },
  {
    title: '被检查单位',
    dataIndex: 'miningTenant',
  },
  {
    title: '检查地点',
    dataIndex: 'location',
    slots: { customRender: 'location' },
  },
  {
    title: '检查时间',
    dataIndex: 'checkTime',
  },
  {
    title: '检查人员',
    dataIndex: 'checkPerson',
    slots: { customRender: 'checkPerson' },
  },
  {
    title: '陪同人员',
    dataIndex: 'accompanyPerson',
    slots: { customRender: 'accompanyPerson' },
  },
  {
    title: '记录人员',
    dataIndex: 'recordPerson',
    slots: { customRender: 'recordPerson' },
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 'NEW',
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 'NEW' : 'START';
          const { createMessage } = useMessage();
          updateTaskStatus({ id: record.id, status: newStatus })
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功检查记录状态`);
            })
            .catch(() => {
              createMessage.error('修改检查记录状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: '企业类别',
    component: 'ApiSelect',
    componentProps: {
      api: getCorpTypeList,
      params: {},
      resultField: 'returnData.list',
      labelField: 'name',
      valueField: 'id',
      immediate: true,
    },
    colProps: { span: 8 },
  },
  {
    field: 'name',
    label: '企业名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'time',
    label: '检查时间',
    component: 'DatePicker',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: 'NEW' },
        { label: '停用', value: 'START' },
      ],
    },
    colProps: { span: 8 },
  },
  {
    field: 'tableName',
    label: '检查表名称',
    component: 'Input',
    colProps: { span: 8 },
  },
];

